package com.example.xmltest.controller;

import com.alibaba.fastjson.JSON;
import com.example.xmltest.dto.*;
import com.example.xmltest.model.UserInfo;
import com.example.xmltest.model.UserInfoItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("xml")
public class TestController {

    @GetMapping("test")
    public String test(){
        return "t-test";
    }

    /**
     * jumeng demo
     * @param juMengDTO1
     * @return
     */
    @PostMapping(value = "callback_jm", consumes = MediaType.APPLICATION_XML_VALUE)
    public String callback_jm(@RequestBody JuMengDTO2 juMengDTO1){
        // return OK 或者 0
        return JSON.toJSONString(juMengDTO1);
    }

    /**
     * 中鼎动力 demo
     * @param paramString
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping(value = "callback_zd",consumes = "text/plain;charset=UTF-8")
    public UserInfo callback_zd(@RequestBody String paramString) throws UnsupportedEncodingException {
        // return RESULT:0 或者 RESULT:1 0：成功，1：失败
        UserInfo userInfo = new UserInfo();
        List<UserInfoItem> itemList = new ArrayList<>();
        userInfo.setItemList(itemList);
        String[] userInfoArr = paramString.split("#");
        if (userInfoArr.length > 1){
            String accountStr = userInfoArr[0];
            userInfo.setAccount(accountStr);
            String userInfoStr = userInfoArr[1];
            String[] phoneInfoArr = userInfoStr.split("\\|");
            for (String str : phoneInfoArr) {
                String decodeStr = URLDecoder.decode(str, "UTF-8");
                String[] info = decodeStr.split(",");
                UserInfoItem item = new UserInfoItem();
                item.setPhone(info[0]);
                item.setTaskId(info[1]);
                item.setStatus(info[2]);
                itemList.add(item);
            }
        }
        return userInfo;
    }

    /**
     * shouwang demo
     * @param list
     * @return
     */
    @PostMapping(value = "callback_sw")
    public String callback_sw(@RequestBody List<ShouWangDTO> list){
        // return {"code": "00000"}
        return JSON.toJSONString(list);
    }

    /**
     * 梦网demo  已有,
     * @param list
     * @return
     */
//    @PostMapping(value = "callback_mw")
//    public String callback_sw(@RequestBody List<ShouWangDTO> list){
//        // return {"code": "00000"}
//        return JSON.toJSONString(list);
//    }

    /**
     * 可以提供两个Url
     * 通了即可，不要求返回
     * @param teleSignDTO
      * @return
     */
    @PostMapping(value = "callback_telesign")
    public String callback_telesign(@RequestBody TeleSignDTO teleSignDTO){
        // return {"code": "00000"}
        return JSON.toJSONString(teleSignDTO);
    }

    /**
     * twilo需要一个回调Url，在回调接口里做更新
     * @param request
     * @return
     */
    @PostMapping(value = "callback_twilo")
    public String callback_twilo(HttpServletRequest request){
        // return {"code": "00000"}
        String messageStatus = request.getParameter("MessageStatus");
        String messageSid = request.getParameter("MessageSid");
        String smsStatus = request.getParameter("SmsStatus");
        return JSON.toJSONString(messageStatus);
    }

    @PostMapping(value = "callback_sendgrid")
    public String callback_sendgrid(HttpServletRequest request,@RequestBody List<SendGridDTO> list){
        byte[] bytes = list.toString().getBytes();
        // return {"code": "00000"}
        return JSON.toJSONString(list);
    }

    @PostMapping(value = "callback_ali")
    public String callback_ali(AliEmailDTO dto){
        // return {"code": "00000"}
        return JSON.toJSONString(dto);
    }

    @PostMapping(value = "test")
    public String test(TwilioSignDTO dto){
        return dto.getAccountSid()+"--"+dto.getApiVersion()+"--"+dto.getFrom()+"--"+dto.getTo()+"--"+dto.getMessageSid();
    }

}
